<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;" name="viewport" />
    <title></title>

    <style type="text/css">
        html,
        body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
        
        #map {
            width: 100%;
            height: 100%;
        }
    </style>
</head>

<body>

    <div id="map"></div>
    <canvas id="canvas"></canvas>

    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=1XjLLEhZhQNUzd93EjU5nOGQ"></script>
    <script type="text/javascript" src="../../build/mapv.js"></script>

    <script type="text/javascript">
    var MARKER_ICON_SIZE_WIDTH = 45
    var MARKER_ICON_SIZE_HEIGHT = 45

    function getMapvEventMarkerIndex(map, event, data){
        var index = null;
        var eventPixel = map.pointToPixel(event.point);

        var XR = MARKER_ICON_SIZE_WIDTH; // 除2暂时没有弄明白，但是这样写就正常了...
        var YR = MARKER_ICON_SIZE_HEIGHT;

        for(var i=0,projsLen=data.length; i< projsLen; i++){
            var itemPixel = map.pointToPixel({lng: data[i].lng, lat: data[i].lat});
            var topX = itemPixel.x + XR;
            var bottomX = itemPixel.x - XR;
            var topY = itemPixel.y + YR;
            var bottomY = itemPixel.y - YR;

            if (eventPixel.x < topX && eventPixel.x > bottomX && eventPixel.y < topY && eventPixel.y > bottomY) {
                index = i;
            }
        }

        return index;
    }
        // 百度地图API功能
        var map = new BMap.Map("map", {
            enableMapClick: false
        }); // 创建Map实例
        map.centerAndZoom(new BMap.Point(105.403119, 38.028658), 5); // 初始化地图,设置中心点坐标和地图级别
        map.enableScrollWheelZoom(true); // 开启鼠标滚轮缩放

        map.setMapStyle({
            style: 'midnight'
        });

        var randomCount = 30;

        var data = [];

        var citys = ["北京", "天津", "上海", "重庆", "石家庄", "太原", "呼和浩特", "哈尔滨", "长春", "沈阳", "济南", "南京", "合肥", "杭州", "南昌", "福州", "郑州", "武汉", "长沙", "广州", "南宁", "西安", "银川", "兰州", "西宁", "乌鲁木齐", "成都", "贵阳", "昆明", "拉萨", "海口"];

        var img = new Image();
        img.src = '//logo.toodc.cn/me/map/map_marker_blue.png';

        img.onload = function() {
            // console.log(img)
            // 构造数据
            while (randomCount--) {
                var cityCenter = mapv.utilCityCenter.getCenterByCityName(citys[parseInt(Math.random() * citys.length)]);
                var lng = cityCenter.lng - 2 + Math.random() * 4
                var lat = cityCenter.lat - 2 + Math.random() * 4
                data.push({
                    geometry: {
                        type: 'Point',
                        coordinates: [lng, lat]
                    },
                    deg: 360 * Math.random(),
                    icon: img,
                    id: randomCount,
                    lng: lng,
                    lat: lat
                });
            }
            var dataSet = new mapv.DataSet(data);

            var options = {
                draw: 'icon',
                methods: {
                    tap: function(item, e){
                        alert('tap: '+  (item ? item.id : null))
                    }
                },
                // methods: {
                //    click: function (item) {
                //        console.log(item);
                //     }
                // },
                size: 45,
                width: 45,
                height: 45,
                // sx: 10,
                // sy: 10,
                // swidth: 50,
                // sheight: 50,
            }

            var mapvLayer = new mapv.baiduMapLayer(map, dataSet, options);

            // var options = {
            //     draw: 'simple'
            // }
            // var mapvLayer = new mapv.baiduMapLayer(map, dataSet, options);
        }
    </script>

</body>

</html>
